package com.lmh.util;

import java.io.*;
import java.net.*;

/**
 * Created by Liumh on 2018/5/25.
 */
public class FileUtil {

    public static char[] readStrFromDisk(String filePath){
        File file = new File(filePath);
        try {
            Reader reader = new FileReader(file);
            int num = reader.read();
            char[] chars = new char[num];
            reader.read(chars);
            reader.close();
            return chars;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static byte[] readStrFromNet(String netUrl){
        try {
            URL url = new URL(netUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            //设置超时时间
            connection.setReadTimeout(2000);
            connection.setRequestProperty("Accept-Charset","GBK");
            connection.setRequestProperty("contentType","GBK");
            //获取输入流
            InputStream inp = connection.getInputStream();
            int num = inp.read();
            byte[] bytes = new byte[num];
            inp.read(bytes);
            inp.close();
            return bytes;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
